/* 
 * Author:   张卫明 
 * Description: 读取缓存模块
 * Revision Time: 2015-4-27
 */

;(function($,window,ECar){

	"use strict";
	

	var SEPERATOR=','; //分割器
	ECar.Tool = {};
	
	/**
	 * 判断是否为IE
	 */
	
	ECar.Tool.isIE6 = /msie 6/i.test(navigator.userAgent);
	ECar.Tool.isIE7 = /msie 7/i.test(navigator.userAgent);
	ECar.Tool.isIE8 = /msie 8/i.test(navigator.userAgent);
	ECar.Tool.isIE = /msie/i.test(navigator.userAgent);
	
	/**
	 * indexOf 兼容IE
	 */
	if(!Array.indexOf)
	{
	    Array.prototype.indexOf = function(obj)
	    {             
	        for(var i=0; i<this.length; i++)
	        {
	            if(this[i]==obj)
	            {
	                return i;
	            }
	        }
	        return -1;
	    }
	}
	
	/**
	 * 判断是否为空
	 */
	ECar.Tool.isNotEmpty = function(obj) {
		return obj != null && $.trim(obj).length > 0;
	};
	
	/**
	 * 读取Cookie
	 */
	ECar.Tool.getCookieBonus = function(key){    
		var arrStr = document.cookie.split(';');
		for(var i = 0 ; i < arrStr.length ; i++){
			var temp = arrStr[i].split('=');
			if($.trim(temp[0]) == key){
				return decodeURIComponent(temp[1]);
			}
		}
		return null;
	}
	
	/**
	 * 写入Cookie
	 */
	ECar.Tool.setCookieBonus = function(setting){
		if(!setting || setting.key==null || setting.val==null) return;
		
		var key = setting.key,val = setting.val,day = setting.day,domain = setting.domain,ExpireDate = new Date();
		
		ExpireDate.setTime(ExpireDate.getTime() + (day*1000*3600*24)); // 设置有效期为day天
		document.cookie = key + "=" + encodeURIComponent(val) +
		((day == null) ? "" : "; expires=" + ExpireDate.toGMTString())+((domain == null) ? "" : ";path=/;domain="+ domain);
	}
	
	/**
	 * 更新 浏览历史 Cookie 并返还最新 浏览历史
	 */
	ECar.Tool.updateHistory =  function(currentProduct){
		var browseHistory = ECar.Tool.getCookieBonus('MALL_HISTORY'),browseHistoryArray,newHistory;
		if(ECar.Tool.isNotEmpty(currentProduct)){
			
			// 浏览历史不为空
			if(ECar.Tool.isNotEmpty(browseHistory)){
				
				browseHistoryArray = browseHistory.split(SEPERATOR);
				
				// 浏览历史不包含当前浏览商品
				if(browseHistoryArray.indexOf(currentProduct)<0){
				
					if(browseHistoryArray.length<8){      
    	        	    // 浏览历史不足8个
    	                newHistory =currentProduct+SEPERATOR+browseHistory;
    	           }else{                              
    	        	    // 浏览历史满足8个	               
    	                newHistory = currentProduct+SEPERATOR+browseHistory.substring(0,browseHistory.lastIndexOf(SEPERATOR));
    	           }
    	           
				// 浏览历史包含当前浏览商品(移动该商品到首位)
				} else {
					for (var i = 0; i < browseHistoryArray.length; i++) {
						if (browseHistoryArray[i] === currentProduct) {
							browseHistoryArray.splice(i, 1);
							break;
						}
					}
					browseHistoryArray.unshift(currentProduct);
					newHistory = browseHistoryArray.join(SEPERATOR);
				}
			// 浏览历史为空
	        } else {
				newHistory = currentProduct;
			}
			
			// 更新浏览历史
			ECar.Tool.setCookieBonus({'key':'MALL_HISTORY','val':newHistory,'day':30});
			
			return newHistory.split(SEPERATOR);
			
		} else {
			return ECar.Tool.isNotEmpty(browseHistory) ? browseHistory.split(SEPERATOR) : null;
		}
		
	}

})(jQuery,window,window.ECar||(window.ECar={}));

//ECar.Tool.updateHistory('100885128');
